<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="../../extjs_all/resources/css/ext-all.css"></link>
<script type="text/javascript" src="../../extjs_all/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../../extjs_all/ext-all.js"></script>
<script type="text/javascript" src="../../extjs_all/source/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript">
Ext.onReady(function (){
	Ext.QuickTips.init();	//初始化Ext.QuickTips，以使得tip提示可用
	Ext.form.Field.prototype.msgTarget='side';		//提示后面显示图片标记
	
	DBSimpleTreePanel = Ext.extend(Ext.tree.TreePanel,{
		menu:null,
		constructor:function(){
			this.menu = new Ext.menu.Menu({
				minWidth:300,
				maxHeight:500, 
				items:[{
					//icon:"icon-info.gif", 
					text:'新增',
					handler:this.onAddNode,
					scope:this
				},{
					text:'删除',
					handler:this.onDelNode,
					scope:this
				},{
					text:'修改',
					handler:this.onUpdateNode,
					scope:this
				}]
			});
			DBSimpleTreePanel.superclass.constructor.call(this,{
				renderTo:Ext.getBody(),
				width:200,
				height:400,
				loader:new Ext.tree.TreeLoader({
					url:'findmenus.action',
					clearOnLoad :false 	//加载时,不删除存在的子节点
					//url:'treedata.js'
				}),
				listeners:{
					'checkchange':{
						fn:this.onCheckChanage,
						scope:this
					}
				},
				root:new Ext.tree.AsyncTreeNode({
					text:'树结构',
					id:'0',
					checked:false
				})
			});
		},
		onCheckChanage:function(node, checked) {
			alert(123);
            node.expand();
            node.attributes.checked = checked;
            node.eachChild(function(child) {
                        child.ui.toggleCheck(checked);
                        child.attributes.checked = checked;
                    })
		},
		onContextMenu:function(node,_e){
			this.menu['crr'] = node;
			if(this.menu['crr'].id == '0'){
				this.menu.items.itemAt(1).setDisabled(true);
			}else{
				this.menu.items.itemAt(1).setDisabled(false);
			}
			this.menu.showAt(_e.getXY());
		},
		onAddNode:function(){
			Ext.Msg.prompt('请输入新增节点名称','新增名称:',this.onAddNodePrompt,this);
		},
		onAddNodePrompt:function(btn,txt){
			if(btn == 'ok'){
				this.menu['crr'].leaf = false;
				var node = this.menu['crr'].appendChild(
					new Ext.tree.AsyncTreeNode({
						text:txt,
						leaf:true,
						id:Ext.id()
					})
				);
				this.menu['crr'].expand();
				
				Ext.Ajax.request({
					url:'addmenus.action',
					params:{
						'menus.text':txt,
						'menus.patentid':this.menu['crr'].id
						
					}
				})
				
			}
		},
		onDelNode:function(){
			Ext.Msg.confirm('系统提示','是否删除此节点?',this.onDelNodeConfirm,this);
		},
		onDelNodeConfirm:function(btn){
			if(btn =='yes'){
				this.menu['crr'].remove();
			}
		},
		onDelNodePrompt:function(btn,txt){
			if(btn == 'ok'){
				this.menu['crr'].setText(txt);
			}
		},
		onUpdateNode:function(){
			Ext.Msg.prompt('请输入修改后的名称','修改名称:',this.onUpdateNodePrompt,this,false,this.menu['crr'].text);
		},
		onUpdateNodePrompt:function(btn,txt){
			if(btn == 'ok'){
				this.menu['crr'].setText(txt);
			}
		}
	})
	new DBSimpleTreePanel();
});
</script>
<title>Tree Panel</title>
</head>
<body>

</body>
</html>